/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2022-09-14 13:53:48
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2022-09-21 14:26:01
 * @FilePath: \robot\src\views\patroling\composables\useDevice.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ElMessage } from "element-plus";
import { ref, computed } from "vue";
import {
  getDevice,
  deleteRobotDevice,
  createRobotDevice,
  updateRobotDevice,
} from "@/api/robot-device.js";
import { getCameraList, setCameraRid } from "@/api/robot-camera.js";
import { getList as getDeviceTypeList } from "@/api/robot-device-type.js";

export default (caritem, getCarLink) => {
  // add：新增，edit：修改
  const currentAction = ref("");
  const deviceSettingVisible = ref(false);
  // 设备类型
  const deviceTypeList = ref([]);
  const cameraList = ref([]);
  // 表单信息
  const deviceFormData = ref([]);
  /**
   * @description: 打开外设信息弹窗
   * @return {*}
   */
  async function openDeviceSetting(type, row = { id: "" }) {
    if (!caritem.value) {
      ElMessage.warning({
        message: "请先选择机器人",
      });
      return;
    }
    currentAction.value = type;
    deviceSettingVisible.value = true;

    const { data } = await getDeviceTypeList();
    deviceTypeList.value = data || [];

    let formData = {};
    formData.number = caritem.value;
    if (currentAction.value === "edit") {
      const { data } = await getDevice(row.id);
      formData = data || {};
      formData.status = Boolean(data.status);

      if (data.type === "ipc") {
        const { data } = await getCameraList({ page: 1, limit: 100 });
        cameraList.value = data.list || [];
        const camera = cameraList.value.find((item) => item.rid === data.rid);
        if (camera) {
          formData.cameraId = camera.id;
        }
      }
    }
    deviceFormData.value = formData;
  }

  // 外设信息弹窗表单
  const deviceFormOptions = computed(() => {
    return [
      {
        prop: "number",
        label: "设备编号",
      },
      {
        prop: "name",
        label: "设备名称",
      },
      {
        prop: "type",
        label: "设备类型",
        type: "select",
        options: deviceTypeList.value.map((item) => ({
          label: item.name,
          value: item.type,
        })),
        events: {
          async change(value) {
            if (value === "ipc") {
              const {
                data: { list = [] },
              } = await getCameraList({ page: 1, limit: 100 });
              cameraList.value = list;
            }
          },
        },
      },
      {
        prop: "cameraId",
        label: "摄像头列表",
        type: "select",
        options: cameraList.value.map((item) => ({
          label: item.name,
          value: item.id,
        })),
        disabled: (formData) => {
          return formData.type !== "ipc";
        },
      },
      {
        prop: "status",
        label: "设备状态",
        type: "switch",
        attributes: {
          activeText: "开启",
          inactiveText: "关闭",
          activeColor: "#ff931e",
        },
      },
      {
        prop: "intranatIp",
        label: "内网IP",
      },
      {
        prop: "intranatPort",
        label: "内网端口",
      },
      {
        prop: "internatIp",
        label: "外网IP",
      },
      {
        prop: "internatPort",
        label: "外网端口",
      },
    ];
  });

  /**
   * @description: 删除设备
   * @param {*} id
   * @return {*}
   */
  async function delDevice(id) {
    await deleteRobotDevice(id);
    ElMessage.success({
      message: "删除成功",
    });
    getCarLink();
  }

  /**
   * @description: 处理表单结果
   * @return {*}
   */
  async function handleDevice(formData) {
    const data = { ...formData };
    data.rid = caritem.value;
    data.number = caritem.value;
    data.status = data.status ? 1 : 0;
    data.isDel = 0;
    data.rtype = "patroling";
    if (currentAction.value === "add") {
      await createRobotDevice(data);
      ElMessage.success({
        message: "提交成功",
      });
    } else if (currentAction.value === "edit") {
      await updateRobotDevice(data);
      ElMessage.success({
        message: "修改成功",
      });
    }
    if (data.type === "ipc") {
      if (!data.cameraId) {
        ElMessage.warning({
          message: "请选择绑定的摄像头",
        });
        return;
      }
      await setCameraRid({
        id: data.cameraId,
        rid: data.rid,
        rtype: "patroling",
      });
      ElMessage.success({
        message: "绑定成功",
      });
    }
    deviceSettingVisible.value = false;
    getCarLink();
  }

  return {
    openDeviceSetting,
    deviceSettingVisible,
    deviceFormOptions,
    deviceFormData,
    delDevice,
    handleDevice,
  };
};
